package cn.haohaowo.controller;

import cn.haohaowo.common.Constants;
import cn.haohaowo.common.Page;
import cn.haohaowo.common.SessionContainer;
import cn.haohaowo.entity.Account;
import cn.haohaowo.entity.Order;
import cn.haohaowo.entity.ShoppingCart;
import cn.haohaowo.service.OrderBizService;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Getter
@Setter
@Controller
@RequestMapping("/order")
public class OrderController {

	private static final long serialVersionUID = 7914890339215335116L;

	private Integer id;
	private Order order;
	private Account account;
	private ShoppingCart shoppingCart;
	private SessionContainer sessionContainer;
	private Integer pageNo;
	private Page<Order> page;
	private Long ordersCompletedCount;
	private Long ordersOnProcessingCount;
	
	@Autowired
	private OrderBizService orderBizService;

    @RequestMapping("/create.do")
	public String create(HttpServletRequest request, HttpServletResponse response) {
		HttpSession session = request.getSession();
		order = (Order) session.getAttribute(Constants.PREPARED_ORDER_KEY);
		if(null != order){
			return Constants.CART;
		}

		sessionContainer = (SessionContainer) session.getAttribute(Constants.SESSION_CONTAINER_KEY);
		shoppingCart = sessionContainer.getShoppingCart();
		order.setStatus(Order.NEW);
		order.setTotalPrice(shoppingCart.getTotalPrice());
		order.setOrderDetailList(shoppingCart.createOrderDetailFromCartItem());
		orderBizService.createOrder(order);

		shoppingCart.clear();
		sessionContainer.setShoppingCart(shoppingCart);
		session.setAttribute(Constants.SESSION_CONTAINER_KEY, sessionContainer);

		session.removeAttribute(Constants.PREPARED_ORDER_KEY);
		session.setAttribute(Constants.PREPARED_ORDER_KEY, order.getNumber());
		
		return Constants.CREATE_ORDER_SUCCESS;
	}

    @RequestMapping("/createSuccess.do")
	public String createSuccess(HttpServletRequest request, HttpServletResponse response){
        HttpSession session = request.getSession();
		order = (Order) session.getAttribute(Constants.PREPARED_ORDER_KEY);
		if (null == order) {
			return Constants.CART;
		} else {
			return Constants.CREATE_ORDER_SUCCESS_PAGE;
		}
	}

    @ResponseBody
    @RequestMapping("/list.do")
	public String list(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        account = JSONObject.parseObject(session.getAttribute(Constants.USER_KEY).toString(), Account.class);
		Integer accountId = account.getId();
		
		Page<Order> page = orderBizService.getCurrentAccountOrders(accountId, pageNo);
		page.setUrl(request.getRequestURL().toString());
		
//		ordersCompletedCount = orderBizService.getCurrentAccountCompletedOrdersCount(accountId);
//		ordersOnProcessingCount = orderBizService.getCurrentAccountOnPrecessingOrdersCount(accountId);
		
		return Constants.ORDER_LIST;
	}

    @RequestMapping("/show.do")
	public String show(HttpServletRequest request, HttpServletResponse response){
        order = orderBizService.getOrder(id);
        if (null == order) {
            request.getSession().setAttribute(Constants.ORDER_KEY, JSONObject.toJSON(order));
        }

		return Constants.SHOW;
	}
	
}
